<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Lang extends AdminBase {
	public function __construct()
    {
        parent::__construct();
    }

	public function index()
	{
		$post = $this->input->post();
		if($post){
			if(isset($post['id'])){//name order 
				if(isset($post['name'])){//修改名称
					if($post['id']==1){
						$this->output->set_content_type('application/json')
						 ->set_output(json_encode(array('statu' => 'failed','message'=>'中文不允许修改！')));
						return;
					}
					if(mb_strlen($post['name'],"UTF8")>20 || mb_strlen($post['name'],"UTF8")<1){
						$this->output->set_content_type('application/json')
						 ->set_output(json_encode(array('statu' => 'failed','message'=>'语言名称为1-20个字符！')));
						return;
					}
					if(!preg_match("/^[a-zA-Z\s]{1,20}+$/",$post['iso'])){
						$this->output->set_content_type('application/json')
						 ->set_output(json_encode(array('statu' => 'failed','message'=>'iso为1-20个字母！')));
						return;
					}
					$this->db->where('id',$post['id']);
					$this->db->update('lang',$post);
					$this->output->set_content_type('application/json')
					 ->set_output(json_encode(array('statu' => 'ok','message'=>'修改成功！')));
					return;
				}else{
					if(isset($post['order'])){//排序
						$this->db->where('id',$post['id']);
						$this->db->update('lang',['order'=>$post['order']]);
						$this->output->set_content_type('application/json')
						 ->set_output(json_encode(array('statu' => 'ok','message'=>'排序成功！')));
						return;
					}else{//删除  检查是否有属于该语言的商品
						if($post['id']==1){
							$this->output->set_content_type('application/json')
							 ->set_output(json_encode(array('statu' => 'failed','message'=>'中文不允许删除！')));
							return;
						}
						$product = $this->db->get_where('location',['lang_id'=>$post['id']])->row_array();
						if($product){
							$this->output->set_content_type('application/json')
							 ->set_output(json_encode(array('statu' => 'failed','message'=>'删除失败，请先修改或删除属于该语言的地区！')));
							return;
						}else{
							@unlink('./static/lang/'.$post['id'].'.json');
							$this->db->delete('lang', array('id' => $post['id']));
							$this->output->set_content_type('application/json')
							 ->set_output(json_encode(array('statu' => 'ok','message'=>'删除成功！')));
							return;
						}
					}
				}
			}else{ //新增
				if(mb_strlen($post['name'],"UTF8")>20 || mb_strlen($post['name'],"UTF8")<1){
					$this->output->set_content_type('application/json')
					 ->set_output(json_encode(array('statu' => 'failed','message'=>'语言名称为1-20个字符！')));
					return;
				}
				if(!preg_match("/^[a-zA-Z\s]{1,20}+$/",$post['iso'])){
					$this->output->set_content_type('application/json')
					 ->set_output(json_encode(array('statu' => 'failed','message'=>'iso为1-20个字母和数字！')));
					return;
				}
				$this->db->select('order');
				$this->db->order_by('order', 'DESC');
				$max = $this->db->get('lang')->row_array();
				$post['order'] = $max?$max['order']+1:0;
				$this->db->insert('lang',$post);
				$this->output->set_content_type('application/json')
				 ->set_output(json_encode(array('statu' => 'ok','message'=>'新增成功！')));
				return;
			}
		}else{
			$this->db->order_by('order','ASC');
	        $lang = $this->db->get('lang')->result_array();
			$this->admin_view('lang_list','语言列表',array('list'=>$lang));
		}
	}

	public function tran($id)
	{
		$post = $this->input->post();
		if($post){
			saveTran($id,$post);
			return $this->admin_message('修改成功！','success');
		}
		$lang = $this->db->get_where('lang',['id'=>$id])->row_array();
		if(!$lang){
			return $this->admin_message('语言不存在！','danger');
		}
		$values = getTran($id);
		$mainTran = mainTran();
		foreach($mainTran as &$tran){
			$tran['value'] = isset($values[$tran['name']])?$values[$tran['name']]:'';
		}
		$this->admin_view('lang_tran','翻译-'.$lang['name'],['list'=>$mainTran]);
	}

	

	

}

